Passed
Push — master ( 4dfa9d...f16e9a )
by thomas
01:52
created

HttpClient.getRequest   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.4705

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 23
ccs 2
cts 9
cp 0.2222
crap 1.4705
rs 9.0856
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 13 2
1 1
var axios = require('axios');
2 1
var MIMEType = require('./mimetype');
3 1
var PKIType = require('./x509/pkitype');
4 1
var ProtoBuf = require('./protobuf');
5 1
var PaymentRequest = ProtoBuf.PaymentRequest;
6
7
function checkContentType(response, expecting) {
8 2
    if (!("content-type" in response.headers)) {
9
        throw new Error("Missing content-type header in response");
10
    }
11 2
    if (response.headers["content-type"] !== expecting) {
12
        throw new Error("Invalid content-type header set by server, request failed");
13
    }
14
}
15
16
/**
17
 *
18
 * @constructor
19
 */
20 1
var HttpClient = function() {
21
};
22
23
/**
24
 *
25
 * @param {string} url
26
 * @param {Validator} validator
27
 * @return Promise of PaymentRequest
28
 */
29 1
HttpClient.prototype.getRequest = function(url, validator) {
30
    return axios({
31
        method: 'get',
32
        headers: {
33
            "Accept": MIMEType.PAYMENT_REQUEST
34
        },
35
        url: url,
36
        responseType: 'arraybuffer'
37
    })
38
        .then(function(response) {
39
            checkContentType(response, MIMEType.PAYMENT_REQUEST);
40
41
            var buf = Buffer.from(response.data);
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
42
            var paymentRequest = PaymentRequest.decode(buf);
43
44
            var path = null;
45 2
            if (paymentRequest.pkiType !== PKIType.NONE) {
46
                path = validator.verifyX509Details(paymentRequest);
47
            }
48
49
            return [paymentRequest, path];
50
        });
51
};
52
53
/**
54
 *
55
 * @param {ProtoBuf.PaymentDetails} details
56
 * @param {string[]} txs - list of transactions fulfilling request
57
 * @param {string|null} memo - optional memo for merchant
58
 * @returns {{transactions: Array}} - Payment object
59
 */
60 1
HttpClient.prototype.preparePayment = function(details, txs, memo) {
61
    var detailKeys = Object.keys(details);
62 2
    if (detailKeys.indexOf("paymentUrl") === -1) {
63
        throw new Error("Not payment url on details");
64
    }
65
66
    var payment = {
67
        transactions: []
68
    };
69
70
    txs.map(function(tx) {
71 2
        if (typeof tx === "string") {
72
            payment.transactions.push(tx);
73
        } else {
74
            throw new Error("txs must be a list of raw transactions");
75
        }
76
    });
77
78 2
    if (detailKeys.indexOf("merchantData") !== -1) {
79
        payment.merchantData = details.merchantData;
80
    }
81
82 2
    if (typeof memo === "string") {
83
        payment.memo = details.memo;
84
    }
85
86
    return payment;
87
};
88
89
/**
90
 *
91
 * @param {ProtoBuf.PaymentDetails} details
92
 * @param {string[]} txs
93
 * @param {string|null} memo
94
 * @returns Promise of PaymentACK
95
 */
96 1
HttpClient.prototype.sendPayment = function(details, txs, memo) {
97
    var payment = this.preparePayment(details, txs, memo);
98
    var paymentData = ProtoBuf.Payment.encode(payment).final();
99
100
    return axios({
101
        method: 'post',
102
        headers: {
103
            "Accept": MIMEType.PAYMENT_ACK,
104
            "Content-Type": MIMEType.PAYMENT
105
        },
106
        url: details.paymentUrl,
107
        data: paymentData,
108
        responseType: 'arraybuffer'
109
    })
110
        .then(function(response) {
111
            checkContentType(response, MIMEType.PAYMENT_ACK);
112
            var buf = Buffer.from(response.data);
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
113
            return ProtoBuf.PaymentACK.decode(buf);
114
        });
115
};
116
117
module.exports = HttpClient;
118